package com.pro_test.proservice.controller;


import com.baomidou.mybatisplus.core.conditions.query.QueryWrapper;
import com.baomidou.mybatisplus.extension.plugins.pagination.Page;
import com.pro_test.commonutils.R;
import com.pro_test.proservice.entity.ProCharge;
import com.pro_test.proservice.entity.ProTemporarypark;
import com.pro_test.proservice.entity.vo.TemporaryparkQuery;
import com.pro_test.proservice.service.ProTemporaryparkService;
import io.swagger.annotations.ApiOperation;
import io.swagger.annotations.ApiParam;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.util.StringUtils;
import org.springframework.web.bind.annotation.*;

import java.util.List;

/**
 * <p>
 *  前端控制器
 * </p>
 *
 * @author Captain
 * @since 2022-02-26
 */
@RestController
@RequestMapping("/proservice/pro-temporarypark")
@CrossOrigin
public class ProTemporaryparkController {

    @Autowired
    private ProTemporaryparkService proTemporaryparkService;

    @GetMapping("findAll")
    public R findAllTemPark()
    {
        List<ProTemporarypark> list = proTemporaryparkService.list(null);
        return R.ok().data("items",list);
    }

    @DeleteMapping("{id}")
    public R removeTemPark(@ApiParam(name = "id", value = "停车ID", required = true)
                        @PathVariable String id)
    {
        boolean flag = proTemporaryparkService.removeById(id);
        if (flag)
        {
            return R.ok();
        }
        else
        {
            return R.error();
        }
    }

    //条件查询分页
    @ApiOperation(value = "条件分页查询")
    @PostMapping("pageTemParkCondition/{current}/{limit}")
    public R pageTemParkCondition(@PathVariable long current,
                               @PathVariable long limit,
                               @RequestBody(required = false) TemporaryparkQuery temporaryparkQuery)
    {
        //创建page对象
        Page<ProTemporarypark> pageTemPark = new Page<>(current,limit);

        //构建条件
        QueryWrapper<ProTemporarypark> wrapper = new QueryWrapper<>();
        String ownerId = temporaryparkQuery.getOwnerId();
        Integer status = temporaryparkQuery.getStatus();
        String begin = temporaryparkQuery.getBegin();
        String end = temporaryparkQuery.getEnd();

        if (!StringUtils.isEmpty(ownerId))
        {
            wrapper.like("owner_id",ownerId);
        }
        if (!StringUtils.isEmpty(status))
        {
            wrapper.eq("status",status);
        }
        if (!StringUtils.isEmpty(begin))
        {
            wrapper.ge("gmt_create",begin);
        }
        if (!StringUtils.isEmpty(end))
        {
            wrapper.le("gmt_create",end);
        }
        //排序
        wrapper.orderByDesc("gmt_create");

        //调用方法实现条件查询分页
        proTemporaryparkService.page(pageTemPark,wrapper);
        long total = pageTemPark.getTotal();
        List<ProTemporarypark> records = pageTemPark.getRecords();

        return R.ok().data("total",total).data("items",records);
    }

    @PostMapping("addTemPark")
    public R addTemPark(@RequestBody ProTemporarypark proTemporarypark)
    {
        boolean save = proTemporaryparkService.save(proTemporarypark);
        if (save)
        {
            return R.ok();
        }
        else
        {
            return R.error();
        }
    }

    @GetMapping("getTemPark/{id}")
    public R getTemPark(@PathVariable String id)
    {
        ProTemporarypark proTemporarypark = proTemporaryparkService.getById(id);
        return R.ok().data("temPark",proTemporarypark);
    }

    @GetMapping("getTemParkByOwnerId/{ownerId}")
    public R getTemParkByOwnerId(@PathVariable String ownerId)
    {
        Page<ProTemporarypark> pageTemporarypark = new Page<>();

        QueryWrapper<ProTemporarypark> wrapper = new QueryWrapper<>();
        if (!StringUtils.isEmpty(ownerId))
        {
            wrapper.eq("owner_id",ownerId);
        }
        proTemporaryparkService.page(pageTemporarypark,wrapper);
        long total = pageTemporarypark.getTotal();
        List<ProTemporarypark> records = pageTemporarypark.getRecords();

        return R.ok().data("total",total).data("proTemporarypark",records);
    }

    @PostMapping("updateTemPark")
    public R updateTemPark(@RequestBody ProTemporarypark proTemporarypark)
    {
        boolean flag = proTemporaryparkService.updateById(proTemporarypark);
        if (flag)
        {
            return R.ok();
        }
        else
        {
            return R.error();
        }
    }

}

